package com.anc.elasticsearch.config;

import feign.Feign;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcRegistrations;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.context.annotation.Primary;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

@Component
@Primary
@ConditionalOnClass({Feign.class})
public class DemoWebMvcRegistrations implements WebMvcRegistrations {

    @Override
    public RequestMappingHandlerMapping getRequestMappingHandlerMapping() {
        return new DemoRequestMappingHandlerMapping();

    }
    /** 注意这个地方不能直接将bean 直接注入到spring 环境，
     * 否则你会发现里面每一个controller 都会被映射2次
     * @see org.springframework.web.servlet.handler.AbstractHandlerMethodMapping
     **/
    public class DemoRequestMappingHandlerMapping extends RequestMappingHandlerMapping implements WebMvcRegistrations {
        @Override
        protected boolean isHandler(Class<?> beanType) {
            return (AnnotatedElementUtils.hasAnnotation(beanType, Controller.class) ||
                    // 当前类上存在RequestMapping 但是不存在FeignClient注解
                    (AnnotatedElementUtils.hasAnnotation(beanType, RequestMapping.class)
                            && !AnnotatedElementUtils.hasAnnotation(beanType, FeignClient.class)
                    ));
        }

    }
//    @Bean
    public WebMvcRegistrations feignWebRegistrations() {
        return new WebMvcRegistrations() {
            @Override
            public RequestMappingHandlerMapping getRequestMappingHandlerMapping() {
                return new RequestMappingHandlerMapping();
            }
        };
    }
//    /**
//     * swagger过滤同时被 @FeignClient和@RequestMapping 修饰的类
//     */
//    private static class FeignRequestMappingHandlerMapping extends RequestMappingHandlerMapping {
//        @Override
//        protected boolean isHandler(Class<?> beanType) {
//            return super.isHandler(beanType) &&
//                    !AnnotatedElementUtils.hasAnnotation(beanType, FeignClient.class);
//        }
//    }

}